package cn.hp.controller;
import cn.hp.entity.Menu;
import cn.hp.service.MenuService;
import cn.hp.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @GetMapping
    public Result find(Menu menu){
        List<Menu> list = menuService.findMenu(menu);
        return Result.success(list);
    }
    @RequestMapping("page")
    public Result page(Menu menu,@RequestParam(value = "page", defaultValue =
            "1") Integer page,@RequestParam(value = "limit", defaultValue = "10") Integer
                               limit){
        PageInfo<Menu> pageInfo = menuService.findMenu(menu, page, limit);
        return Result.success(pageInfo);
    }
    @PostMapping
    public Result insert(Menu menu){
        int i = menuService.saveMenu(menu);
        return i > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }
    @PutMapping
    public Result update(Menu menu){
        int i = menuService.modifyMenu(menu);
        return i > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        int i = menuService.removeMenuById(id);
        return i > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }
}